﻿// COPYRIGHT 2012, Justin Michalczak
//
// This file is licenced to the public under the Apache
// License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CopperVineSoftware.Stackr.Parser.Lexemes
{
    /// <summary>Definition of a function.</summary>
    /// <remarks>
    ///     A function is a sequence of statements which can be executed via its bound name.
    /// </remarks>
    public class FunctionDefinition : Definition
    {
        private readonly string name;
        private readonly IEnumerable<Statement> body;

        /// <summary>The name to bind the function to.</summary>
        public string Name
        {
            get
            {
                return this.name;
            }
        }

        /// <summary>The sequence of statements defining the function body.</summary>
        public IEnumerable<Statement> Body
        {
            get
            {
                return this.body;
            }
        }

        /// <summary>Constructor.</summary>
        /// <param name="name">Name to bind the function to.</param>
        /// <param name="body">Statements to execute as the function body.</param>
        /// <exception cref="ArgumentNullException">If the <paramref name="name"/> parameter is <c>null</c> or empty.</exception>
        /// <exception cref="ArgumentNullException">If the <paramref name="body"/> parameter is <c>null</c>.</exception>
        public FunctionDefinition(string name, IEnumerable<Statement> body)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");
            if (body == null)
                throw new ArgumentNullException("statements");

            this.name = name;
            this.body = body;
        }

        /// <summary>Determines whether two function definitions are equal.</summary>
        /// <remarks>
        ///     Two function definitions are considered equal if and only if
        ///     they are bound to the same name and they define in their body
        ///     the same statements in the same order.
        /// </remarks>
        /// <param name="obj">Object to test for equality.</param>
        /// <returns>True if the <paramref name="obj"/> parameter is equal to this function definition.</returns>
        public override bool Equals(object obj)
        {
            if (!(obj is FunctionDefinition))
                return false;

            FunctionDefinition rhs = obj as FunctionDefinition;
            if (this.name != rhs.name || this.body.Count() != rhs.body.Count())
                return false;

            IEnumerator<Statement> thisIterator = this.body.GetEnumerator();
            IEnumerator<Statement> rhsIterator = rhs.body.GetEnumerator();
            while (thisIterator.MoveNext() && rhsIterator.MoveNext())
            {
                if (!(thisIterator.Current.Equals(rhsIterator.Current)))
                    return false;
            }

            return true;
        }

        /// <summary>Retrieves a hash value for this function definition.</summary>
        /// <returns>A hash code for this function definition.</returns>
        public override int GetHashCode()
        {
            return this.body.Select<Statement, int>((l) => l.GetHashCode()).Aggregate(this.name.GetHashCode(), (current, accumulation) => current ^ accumulation);
        }

        /// <summary>Retrieves a string representation of this function definition.</summary>
        /// <returns>A string representation of this function definition.</returns>
        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine(string.Format("{0}: {{ {1} }}", this.name,
                string.Join(" ", this.body.Select<Statement, string>((l) => l.ToString()))));

            return stringBuilder.ToString();
        }
    }
}
